package com.example.logindemo.controller;

import com.example.logindemo.entity.User;
import com.example.logindemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 获取当前用户信息
    @GetMapping("/profile")
    public ResponseEntity<?> getUserProfile(Principal principal) {
        try {
            // 从Principal获取当前登录用户名
            String username = principal.getName();
            
            // 获取用户信息
            User user = userService.getUserByUsername(username);
            
            if (user == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND)
                        .body(Map.of("success", false, "message", "用户不存在"));
            }
            
            // 构建响应数据，不返回密码等敏感信息
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("id", user.getId());
            userInfo.put("username", user.getUsername());
            userInfo.put("email", user.getEmail());
            userInfo.put("phoneNumber", user.getPhoneNumber());
            userInfo.put("registTime", user.getRegistTime());
            userInfo.put("updataTime", user.getUpdataTime());
            
            return ResponseEntity.ok(Map.of("success", true, "data", userInfo));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("success", false, "message", "获取用户信息失败: " + e.getMessage()));
        }
    }

    // 上传用户头像
    @PostMapping("/avatar")
    public ResponseEntity<?> uploadAvatar(Principal principal, @RequestPart("file") MultipartFile file) {
        try {
            // 从Principal获取当前登录用户名
            String username = principal.getName();
            
            System.out.println("收到头像上传请求，用户: " + username + ", 文件: " + file.getOriginalFilename() + ", 大小: " + file.getSize());
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "头像功能暂时不可用");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.err.println("头像上传错误: " + e.getMessage());
            e.printStackTrace();
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "头像功能暂时不可用: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    // 更新用户信息
    @PutMapping("/profile")
    public ResponseEntity<?> updateUserProfile(Principal principal, @RequestBody User userInfo) {
        try {
            // 从Principal获取当前登录用户名
            String username = principal.getName();
            
            // 更新用户信息
            User updatedUser = userService.updateUserInfo(username, userInfo);
            
            // 构建响应数据
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "用户信息更新成功");
            
            Map<String, Object> userData = new HashMap<>();
            userData.put("id", updatedUser.getId());
            userData.put("username", updatedUser.getUsername());
            userData.put("email", updatedUser.getEmail());
            userData.put("phoneNumber", updatedUser.getPhoneNumber());
            userData.put("updataTime", updatedUser.getUpdataTime());
            
            response.put("data", userData);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("success", false, "message", "更新用户信息失败: " + e.getMessage()));
        }
    }
}